package com.keyancompany.zhongbaopingtai.api;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.ConstansUrl;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.ZBApplication;
import com.keyancompany.zhongbaopingtai.activity.WelcomeActivity;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.listener.LoadingHanlder;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.loopj.android.http.RequestParams;

/**
 * Created by firefox on 2016/6/17.
 */
public class UserAPI extends BaseAPI {
    public static final int LOGOIN_CODE = 0X111;
    public static final int REGISTER_CODE = 0X112;
    public static final int GET_MSCODE_CODE = 0X113;
    public static final int RESET_PWD_CODE = 0X114;
    public static final int UESER_INFO_CODE = 0X115;
    public static final int GET_HEAD_PIC_CODE = 0X116;
    public static final int CASH_REQUEST_CODE = 0X117;
    public static final int DISPUTES_LIST = 0X118;
    public static final int EDIT_USER_INFO = 0x119;
    public static final int DISPUTES_DETAIL = 0x120;
    public static final int ATTESTATION_FIRM = 0x121;
    public static final int ATTESTATION = 0x122;
    public static final int EDIT_HEAD_PIC = 0x123;
    public static final int CASH_ACCOUNT = 0x124;
    public static final int SEARCH_COMMENT = 0x125;
    public static final int COMMENT_SUBMIT = 0x126;
    public static final int BIT_TYPE = 0x127;
    public static int PAY_LOGS = 0x128;
    public static final int FENLEI = 0x29;


    private static User currentUser;
    private static String role;


    public static User getCurrentUser() {
        Context context = ZBApplication.getInstance();
        if (currentUser == null) {
            String value = SPUtils.getComment(context).getString(
                    Constants.USER, null);
            if (TextUtils.isEmpty(value)) {
                JumpMain();
                return null;
            }
            currentUser = JSONObject.parseObject(value, User.class);
        }
        return currentUser;
    }


    public static void logoutUser() {
        Context context = ZBApplication.getInstance();
        SPUtils.putCommData(context, Constants.IS_FROM_MAIN, false);
        SPUtils.putCommData(context, Constants.USER_ID, "");
        SPUtils.putCommData(context, Constants.USER, "");
        currentUser = null;
        boolean isFirstInMain = (boolean) SPUtils.getCommData(context, Constants.IS_FROM_MAIN, false);

        // 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
        String value = (String) SPUtils.getCommData(context,
                Constants.USER, "");
        Log.e("hpt", "  logoutUser ____________________ " + value + " ///// " + isFirstInMain);
    }

    public static String getUserId() {
//        if (currentUser == null) {
//            currentUser = getCurrentUser();
//        }
//        return currentUser == null ? "" : "" + currentUser.getUserId();
        Context context = ZBApplication.getInstance();

        String userId = SPUtils.getComment(context).getString(
                Constants.USER_ID, "");

        return userId;
    }

    public static String getRole() {
        if (currentUser == null) {
            currentUser = getCurrentUser();
        }
        return currentUser == null ? "" : "" + currentUser.getRole();
    }

    public static String getTeamCode() {

        Context context = ZBApplication.getInstance();

        String teamCode = SPUtils.getComment(context).getString(
                Constants.TEAM_CODE, null);

        return teamCode;
    }


    /**
     * 用户登录
     *
     * @param tel 手机号 18600624024
     * @param pwd 密码   123456
     */

    public static final void Login(String tel, String pwd, IAsyncHttpResponseHandler handler, Handler loading) {

        RequestParams params = new RequestParams();
        params.put("tel", tel);
        params.put("pwd", pwd);
        post(ConstansUrl.LOGIN, params, handler, LOGOIN_CODE, loading);
    }

    /**
     * 用户登录
     * role：1组织/0个人
     * tel:手机
     * pwd:密码
     * sms:短信
     * accountname:账号
     */
    public static final void register(int role, String tel,
                                      String pwd, String sms,
                                      String accountname, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("role", role + "");
        params.put("tel", tel);
        params.put("pwd", pwd);
        params.put("sms", sms);
        params.put("accountname", accountname);
        post(ConstansUrl.REGISTER, params, handler, REGISTER_CODE, loading);
    }

    public static final void getMscode(String tel, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("tel", tel);
        post(ConstansUrl.GET_MSCODE, params, handler, GET_MSCODE_CODE, loading);
    }

    public static final void resetPwd(String tel, String sms, String pwd, String newpwd, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("tel", tel);
        params.put("sms", sms);
        params.put("pwd", pwd);
        params.put("newpwd", newpwd);

        post(ConstansUrl.RESET_PWD, params, handler, RESET_PWD_CODE, loading);
    }

    public static final void userInfo(IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        post(ConstansUrl.UESER_INFO, params, handler, UESER_INFO_CODE, loading);
    }

    /**
     * 获取头像
     *
     * @param userId
     * @param handler
     * @param loading
     */
    public static final void getHeadpic(String userId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", userId);
        post(ConstansUrl.GET_HEAD_PIC, params, handler, GET_HEAD_PIC_CODE, loading);
    }

    /**
     * 获取头像
     *
     * @param userId
     * @param handler
     * @param loading
     */
    public static final void uploadHeadpic(String userId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", userId);
        post(ConstansUrl.GET_HEAD_PIC, params, handler, GET_HEAD_PIC_CODE, loading);
    }

    /**
     * 提现申请
     *
     * @param handler
     * @param loading
     */
    public static final void cashRequest(String cash_money,
                                         String cash_pwd, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("cash_money", cash_money);
        params.put("cash_pwd", cash_pwd);

        post(ConstansUrl.CASH_REQUEST, params, handler, CASH_REQUEST_CODE, loading);
    }

    /**
     * 修改支付密码
     *
     * @param old_pw
     * @param pw
     * @param handler
     * @param loading
     */
    public static final void editCashPw(String old_pw,
                                        String pw, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("oldPassword", old_pw);
        params.put("newPassword", pw);

        post(ConstansUrl.EDIT_CASH_PW, params, handler, CASH_REQUEST_CODE, loading);
    }


    /**
     * 编辑用户信息
     *
     * @param userNick
     * @param introduction
     * @param handler
     * @param loading
     */
    public static final void editUserInfo(String userNick, String introduction,
                                          IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        editUser(userNick, introduction, null, handler, loading);
    }

    /**
     * 编辑用户信息
     *
     * @param userNick
     * @param introduction
     * @param handler
     * @param loading
     */
    private static final void editUser(String userNick, String introduction, String zhifubao,
                                       IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        //userId：用户id
        // username:昵称
        params.put("userId", getUserId());
        if (!TextUtils.isEmpty(userNick))
            params.put("username", userNick);
        if (!TextUtils.isEmpty(introduction))
            params.put("resume", introduction);
        if (!TextUtils.isEmpty(zhifubao))
            params.put("zhifubao", zhifubao);
        post(ConstansUrl.EDIT_USER_INFO, params, handler, EDIT_USER_INFO, loading);
    }

    /**
     * 编辑用户信息
     *
     * @param zhifubao
     * @param handler
     * @param loading
     */
    public static final void editUserPayAccount(String zhifubao,
                                                IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        //userId：用户id
        // username:昵称
        params.put("userId", getUserId());
        editUser(null, null, zhifubao, handler, loading);
    }

    public static final void updateHeadPic(String pid, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        //userId：用户id
        // username:昵称
        params.put("userId", getUserId());
        params.put("pid", pid);
        post(ConstansUrl.EDIT_USER_INFO, params, handler, EDIT_USER_INFO, loading);
    }

    /**
     * 申述
     *
     * @param handler
     * @param loading
     */
    public static final void complaining(String currentPage, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        //userId：用户id
        // username:昵称
        params.put("userId", getUserId());
        params.put("role", "" + currentPage);
        post(ConstansUrl.DISPUTES_LIST, params, handler, DISPUTES_LIST, loading);
    }

    /**
     * 申诉详情
     *
     * @param id
     * @param handler
     * @param loading
     */
    public static final void complainDetail(String id, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        //userId：用户id
        // username:昵称
        params.put("userId", getUserId());
        params.put("id", id);
        post(ConstansUrl.DISPUTES_DETAIL, params, handler, DISPUTES_DETAIL, loading);

    }

    /**
     * 个人认证
     *
     * @param name          =姓名
     * @param papers_num    =证件号码
     *                      //     *@param passport_num =护照号码
     * @param seachprov     =省
     * @param seachcity     =市
     * @param seachdistrict =区
     * @param papers_pic    =papers_pic=证件照pid(pid,pid)
     * @param address       =地址
     * @param handler
     * @param loading
     */
    public static final void attestation(String name, String papers_num, String seachprov, String seachcity,
                                         String seachdistrict, String papers_pic, String address, IAsyncHttpResponseHandler handler,
                                         LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        params.put("name", name);
        //证件类型 身份证
        params.put("papers_type", "04ec8283b7954d208a51b8fbde6366b1");
        params.put("papers_num", papers_num);
        params.put("seachprov", seachprov);
        params.put("seachcity", seachcity);
        params.put("seachdistrict", seachdistrict);
        params.put("papers_pic", papers_pic);
        params.put("address", address);
        post(ConstansUrl.ATTESTATION, params, handler, ATTESTATION, loading);
    }

    /**
     * firm_name=企业中文名称
     * firm_english=企业英文名称
     * institution=组织机构代码/统一社会信用代码
     * 营业执照电子版
     * capital=注册资本
     * scale=企业规模
     * contact_tel=联系电话
     * contact_fax=联系传真
     * seachprov=省
     * seachcity=市
     * seachdistrict=区
     * address=地址
     */
    public static final void attestationFirm(String frim_name, String firm_english, String institution,
                                             String papers_pic, String capital, String scale, String contact_tel,
                                             String contact_fax, String seachprov, String seachcity,
                                             String seachdistrict, String address, IAsyncHttpResponseHandler handler,
                                             LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        params.put("frim_name", frim_name);
        params.put("firm_english", firm_english);
        if (!TextUtils.isEmpty(institution))
            params.put("institution", institution);
        params.put("capital", capital);
        if (!TextUtils.isEmpty(institution))
            params.put("scale", scale);
        params.put("contact_tel", contact_tel);
        params.put("contact_fax", contact_fax);
        params.put("seachprov", seachprov);
        params.put("seachcity", seachcity);
        params.put("seachdistrict", seachdistrict);
        if (!TextUtils.isEmpty(papers_pic))
            params.put("papers_pic", papers_pic);
        params.put("address", address);
        post(ConstansUrl.ATTESTATION_FIRM, params, handler, ATTESTATION_FIRM, loading);
    }


    /**
     * @param projectpid
     * @param current
     * @param handler
     * @param loadingHanlder
     */
    public static void searchComment(String projectpid, String current, IAsyncHttpResponseHandler handler, LoadingHanlder loadingHanlder) {
        RequestParams params = new RequestParams();
        params.put("projectpid", projectpid);
        params.put("current", current);
        params.put("pageSize", "" + DETAULT_COUNT);

        post(ConstansUrl.SEARCH_COMMENT, params, handler, SEARCH_COMMENT, loadingHanlder);

    }

    public static void submitComment(String projectpid, String commentmsg, String answerpid, IAsyncHttpResponseHandler handler, LoadingHanlder loadingHanlder) {

        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        params.put("projectpid", projectpid);
        params.put("commentmsg", commentmsg);
        params.put("answerpid", answerpid);

        post(ConstansUrl.COMMENT_SUBMIT, params, handler, COMMENT_SUBMIT, loadingHanlder);

    }

    /**
     * 移动端竞标
     * userId：用户pid
     * pid：项目主键
     * isNiMing：是否匿名（0否1是）
     * type：类型（默认手机端0）
     * isteam：是否团队报名（个人0团队1）
     * projectBudget：项目预算
     * projectCycle：预计周期
     */
    public static void bitType(String pid, int isNiMing, int isTeam, String projectBudget, String projectCycle, IAsyncHttpResponseHandler handler, LoadingHanlder loadingHanlder) {

        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        params.put("type", "0");
        params.put("isNiMing", isNiMing + "");
        params.put("pid", pid);
        params.put("isteam", isTeam + "");
        params.put("projectBudget", projectBudget);
        params.put("projectCycle", projectCycle);

        post(ConstansUrl.BIT_TYPE, params, handler, BIT_TYPE, loadingHanlder);
    }

    /**
     * 资金流水
     *
     * @param current
     * @param handler
     * @param loadingHanlder
     */
    public static void paylogs(String current,
                               IAsyncHttpResponseHandler handler, LoadingHanlder loadingHanlder) {
        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        params.put("current", current);
        params.put("pageSize", DETAULT_COUNT + "");
        post(ConstansUrl.PAY_LOGS, params, handler, PAY_LOGS, loadingHanlder);
    }

    public static void fenlei(String parentid,
                              IAsyncHttpResponseHandler handler, LoadingHanlder loadingHanlder) {
        RequestParams params = new RequestParams();
        params.put("userId", getUserId());
        params.put("parentid", parentid);
        get(ConstansUrl.FENLEI, params, handler, FENLEI, loadingHanlder);
    }
}
